﻿using AutoMapper;
using Compass.PurchaseService.Domain;
using Compass.PurchaseService.Domain.Entities;
using Compass.PurchaseService.Infrastructure;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Charts;
using Compass.Wasm.Shared.Purchases;

namespace Compass.Wasm.Server.Services.Purchases;
public interface INavInvReportClassService:IBaseService<NavInvReportClassDto>
{
    Task<ApiResponse<NavInvReportClassDto>> GetSingleByMonthAsync(DateTime month);

    Task<ApiResponse<IEnumerable<ChartDoubleDataDto>>> GetBlazorFsSubtotalAsync();
}
public class NavInvReportClassService:INavInvReportClassService
{
    #region ctor
    private readonly PurchaseDbContext _context;
    private readonly PurchaseDbContext _dbContext;
    private readonly IPurchaseRepository _repository;
    private readonly IMapper _mapper;

    public NavInvReportClassService(PurchaseDbContext dbContext, IPurchaseRepository repository, IMapper mapper)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
    } 
    #endregion
    
    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<NavInvReportClassDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetNavInvReportClassesAsync();
            //按yearMonth排序
            var dtos = await _mapper.ProjectTo<NavInvReportClassDto>(models).OrderBy(x => x.YearMonth).ToListAsync();
            return new ApiResponse<IEnumerable<NavInvReportClassDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<NavInvReportClassDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavInvReportClassDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetNavInvReportClassByIdAsync(id);
            if (model == null) return new ApiResponse<NavInvReportClassDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<NavInvReportClassDto>(model);
            return new ApiResponse<NavInvReportClassDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportClassDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavInvReportClassDto>> AddAsync(NavInvReportClassDto dto)
    {
        try
        {
            var model = new NavInvReportClass(Guid.NewGuid(),  dto.YearMonth, dto.RawMtlValue, dto.SemiProdValue, dto.FgTradeValue,dto.FsSubtotal,dto.MTPT,dto.RecvValue,dto.ConsValue,dto.Gap);
            await _dbContext.NavInvReportClasses.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<NavInvReportClassDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportClassDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavInvReportClassDto>> UpdateAsync(Guid id, NavInvReportClassDto dto)
    {
        try
        {
            var model = await _repository.GetNavInvReportClassByIdAsync(id);
            if (model == null) return new ApiResponse<NavInvReportClassDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);
            return new ApiResponse<NavInvReportClassDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportClassDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavInvReportClassDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetNavInvReportClassByIdAsync(id);
            if (model == null) return new ApiResponse<NavInvReportClassDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<NavInvReportClassDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportClassDto> { Status = false, Message = e.Message };
        }
    }

    #endregion

    #region 扩展查询
    public async Task<ApiResponse<NavInvReportClassDto>> GetSingleByMonthAsync(DateTime month)
    {
        try
        {
            var model = await _repository.GetNavInvReportClassByMonthAsync(month);
            if (model == null) return new ApiResponse<NavInvReportClassDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<NavInvReportClassDto>(model);
            return new ApiResponse<NavInvReportClassDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavInvReportClassDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<IEnumerable<ChartDoubleDataDto>>> GetBlazorFsSubtotalAsync()
    {
        try
        {
            var models = await _repository.GetNavInvReportClassesAsync();

            var dtos = models.Select(x => new ChartDoubleDataDto
            {
                Label = x.YearMonth.ToString("yyyy-MM"),
                Value = x.FsSubtotal
            });

            return new ApiResponse<IEnumerable<ChartDoubleDataDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<ChartDoubleDataDto>> { Status = false, Message = e.Message };
        }
    }

    #endregion


}